home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / winsock / wschesb1.zip / SRC / DDECOMMS.C < prev    next >
C/C++ Source or Header  |  1994-03-15  |  8KB  |  247 lines

  1. /*
  2.   C source for Winsock Chess
  3.   
  4.   Revision 1994-03-15
  5.   Modified by Donald Munro for use as a 2 player chess game over a 
  6.   WINSOCK layer on a TCP (or other WinSock supporting) network.
  7.   Source code and make files for MS Visual C/C++ V1.00/1.50.
  8.   February/March 1994
  9.   All GNU copyright and distribution conditions as described below and in the
  10.   file COPYING also apply to WinSock Chess.
  11.   This module is Winsock Chess specific.
  12.  
  13.   C source for GNU CHESS
  14.   
  15.   Revision: 1990-09-30
  16.   Modified by Daryl Baker for use in MS WINDOWS environment
  17.  
  18.   Copyright (C) 1986, 1987, 1988, 1989, 1990 Free Software Foundation, Inc.
  19.   Copyright (c) 1988, 1989, 1990  John Stanback
  20.  
  21.   This file is part of CHESS.
  22.  
  23.   CHESS is distributed in the hope that it will be useful, but WITHOUT ANY
  24.   WARRANTY.  No author or distributor accepts responsibility to anyone for
  25.   the consequences of using it or for whether it serves any particular
  26.   purpose or works at all, unless he says so in writing.  Refer to the CHESS
  27.   General Public License for full details.
  28.  
  29.   Everyone is granted permission to copy, modify and redistribute CHESS, but
  30.   only under the conditions described in the CHESS General Public License.
  31.   A copy of this license is supposed to have been given to you along with
  32.   CHESS so you can know your rights and responsibilities.  It should be in a
  33.   file named COPYING.  Among other things, the copyright notice and this
  34.   notice must be preserved on all copies.
  35. */
  36.  
  37. #define NOATOM         
  38. #define NOCREATESTRUCT
  39. #define NOFONT
  40. #define NOREGION
  41. #define NOSOUND
  42. #define NOWH
  43. #define NOKANJI
  44.  
  45. #define STRICT 
  46. #include <windows.h>         
  47. #include <windowsx.h>
  48. #include <string.h>  
  49. #include <ddeml.h>
  50. #include <commdlg.h>
  51.  
  52. #include "gnuchess.h"
  53. #include "defs.h"    
  54. #include "chess.h"
  55.  
  56. extern DWORD idDdeServInst,idDdeClntInst;
  57. extern HCONV hconvHost,hconvClient;
  58. extern HWND hwndHostDlg,hwndMain;               
  59. extern BOOL bHost,bConnected,bWaiting,bMoveReady;
  60. extern int User_Move;    
  61. extern HINSTANCE hInst;
  62. extern void (*SendMove)(WORD);
  63. extern HSZ hszServName,hszTopic,hszServNameCl,hszTopicCl,hszItem,hszGet;
  64.  
  65.  
  66. extern MoveInfo moveinfo;
  67. static DWORD    dwXactId;
  68.  
  69. HDDEDATA CALLBACK __export DdeServerProc(UINT type, UINT fmt, HCONV hconv, HSZ hsz1,
  70.                                 HSZ hsz2, HDDEDATA hData, DWORD dwData1,
  71.                                 DWORD dwData2)
  72. //------------------------------------------------------------------------                                
  73. { LPSTR lpszGetBuf;
  74.   HDDEDATA hddedata;
  75.   
  76.   switch (type)
  77.     { case XTYP_CONNECT :
  78.         if ( (hsz1 == hszTopic) || (hsz1 == hszTopicCl) )
  79.           if ( (hwndHostDlg == NULL) && (bHost == TRUE) ) 
  80.             return FALSE;
  81.           else  
  82.             return (HDDEDATA)TRUE;
  83.         else
  84.           return (HDDEDATA)FALSE;  
  85.           
  86.       case XTYP_CONNECT_CONFIRM :
  87.         hconvHost = hconv;
  88.         if (hwndHostDlg != NULL)
  89.            SendMessage(hwndHostDlg,WM_CONNECTED,NULL,NULL);
  90.         return (HDDEDATA)NULL;
  91.         
  92.       case XTYP_REQUEST :   
  93.         if (hsz2 == hszGet)
  94.          { lpszGetBuf = EncodeGame();
  95.            hddedata = DdeCreateDataHandle(idDdeServInst,lpszGetBuf,
  96.                                           4096,0,hszGet,CF_TEXT,0);
  97.            GlobalFreePtr(lpszGetBuf);
  98.            player = opponent;
  99.            User_Move = TRUE;
  100.            return hddedata;
  101.          }                                     
  102.         if (bMoveReady)
  103.          { bMoveReady = FALSE;
  104.            return DdeCreateDataHandle(idDdeServInst,&moveinfo,
  105.                                       sizeof(MoveInfo),0,hszItem,CF_TEXT,0);
  106.          }
  107.         else
  108.           return (HDDEDATA)CBR_BLOCK;                               
  109.         break;
  110.         
  111.       case XTYP_DISCONNECT :
  112.         bConnected = bHost = FALSE;
  113.         SetWindowText(hwndMain,"Winsock Chess (Not Connected)");
  114.         hconvHost = hconvClient = NULL;
  115.         return (HDDEDATA)NULL;           
  116.     }
  117.   return (HDDEDATA)NULL;  
  118. }            
  119.         
  120.  
  121. HDDEDATA CALLBACK __export DdeClientProc(UINT type, UINT fmt, HCONV hconv, HSZ hsz1,
  122.                                 HSZ hsz2, HDDEDATA hData, DWORD dwData1,
  123.                                 DWORD dwData2)
  124. //------------------------------------------------------------------------                                
  125. { switch (type)
  126.    { case XTYP_XACT_COMPLETE :
  127.        if ( (dwData1 == dwXactId) && (hsz2 == hszItem) )
  128.          { DdeGetData(hData,(LPBYTE)&moveinfo,sizeof(MoveInfo),0L);
  129.            bWaiting = TRUE;
  130.            SendMessage(hwndMain,MSG_COMPUTER_MOVE,NULL,NULL);
  131.          }
  132.        return (HDDEDATA)NULL;    
  133.          
  134.      case XTYP_DISCONNECT :
  135.         bConnected = bHost = FALSE;
  136.         hconvHost = hconvClient = NULL;
  137.         return (HDDEDATA)NULL;               
  138.    }
  139.  return (HDDEDATA)NULL;  
  140. }           
  141.        
  142.  
  143. BOOL InitialiseDDE(void)
  144. //----------------------
  145. { BOOL bServInit,bClientInit;
  146.   FARPROC lpDdeServer,lpDdeClient;  
  147.  
  148.   lpDdeServer = MakeProcInstance((FARPROC)DdeServerProc,hInst);       
  149.   lpDdeClient = MakeProcInstance((FARPROC)DdeClientProc,hInst);       
  150.   bServInit    = DdeInitialize(&idDdeServInst,(PFNCALLBACK)lpDdeServer,
  151.                  CBF_FAIL_EXECUTES | CBF_FAIL_POKES | CBF_FAIL_SELFCONNECTIONS,
  152.                  0L);
  153.   bClientInit  = DdeInitialize(&idDdeClntInst,(PFNCALLBACK)lpDdeClient,
  154.                  CBF_FAIL_EXECUTES | CBF_FAIL_POKES | CBF_FAIL_SELFCONNECTIONS, 
  155.                  0L);
  156.   hszServName   = DdeCreateStringHandle(idDdeServInst,"WSCHESSH",NULL);
  157.   hszTopic      = DdeCreateStringHandle(idDdeServInst,"MOVES",NULL);
  158.   hszServNameCl = DdeCreateStringHandle(idDdeServInst,"WSCHESSC",NULL);
  159.   hszTopicCl    = DdeCreateStringHandle(idDdeClntInst,"MOVES",NULL);
  160.   hszItem       = DdeCreateStringHandle(idDdeServInst,"MOVE",NULL);
  161.   hszGet        = DdeCreateStringHandle(idDdeServInst,"GET",NULL);
  162.   if (! bServInit) 
  163.     if (bHost)
  164.      if (DdeNameService(idDdeServInst,hszServName,(HSZ)NULL,DNS_REGISTER) == 0)
  165.        return FALSE;
  166.      else;
  167.     else
  168.      if (DdeNameService(idDdeServInst,hszServNameCl,(HSZ)NULL,DNS_REGISTER) == 0)
  169.        return FALSE;
  170.   return !(bServInit && bClientInit);
  171. }                               
  172.  
  173.  
  174. void DisconnectDDE(void)
  175. //----------------------
  176. { DdeDisconnect(hconvHost);
  177.   DdeDisconnect(hconvClient);
  178. }  
  179.  
  180. void GetOpponentsMoveDDE(WORD cbBytes)
  181. //------------------------------------
  182. { HDDEDATA hddedata;
  183.   
  184.  
  185.   hddedata = DdeClientTransaction(NULL,0,hconvClient,hszItem,CF_TEXT,
  186.                                   XTYP_REQUEST, TIMEOUT_ASYNC,&dwXactId);
  187. }
  188.  
  189. void SendMoveDDE(WORD cbBytes) 
  190. //----------------------------
  191. { bMoveReady = TRUE;
  192.   DdeEnableCallback(idDdeServInst,NULL,EC_ENABLEALL);
  193. }  
  194.  
  195. void GetGameDDE(void)
  196. //-------------------
  197. { char szFileName[256];
  198.  
  199.   if (GetFileName(szFileName,OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST,
  200.                   MSG_CHESS_GET))
  201.    { NewGame(hwndMain);                            
  202.      GetGame(hwndMain,szFileName);               
  203.      lstrcpy(moveinfo.szMove,"COMG\r\n");
  204.      (*SendMove)(7);
  205.    }   
  206. }   
  207.  
  208. void NewGameDDE(void)
  209. //-------------------
  210. { short opp;
  211.  
  212.   opp = opponent;
  213.   lstrcpy(moveinfo.szMove,"COMN\r\n");
  214.   (*SendMove)(7);
  215.   NewGame(hwndMain);
  216.   if (opp == black)
  217.    { computer = white;
  218.      opponent = black;
  219.      User_Move = FALSE;
  220.      flag.reverse = !flag.reverse;   
  221.      PostMessage ( hwndMain, MSG_COMPUTER_MOVE, NULL, (LPARAM)NULL);
  222.    }
  223.   else
  224.    { computer = black;
  225.      opponent = white;
  226.      User_Move = TRUE;
  227.    }  
  228.   UpdateDisplay(hwndMain,0,0,1,0);
  229.   flag.force = false;
  230.   Sdepth = 0; 
  231. }  
  232.  
  233. void SetTimeDDE(int id)
  234. //---------------------
  235. { short sKeepTCmoves,sKeepTCtime;     
  236.  
  237.   
  238.   sKeepTCmoves = TCmoves;
  239.   sKeepTCtime  = TCminutes;  
  240.   if ( TimeControlDialog (hwndMain, hInst, id) ) 
  241.    { TCflag = (TCmoves>1);
  242.      if (TCflag)
  243.        SetTimeControl ();
  244.      wsprintf(moveinfo.szMove,"COMT%3d%3d\r\n",TCmoves,TCminutes);
  245.      (*SendMove)(12);     
  246.    }  
  247. }